image: node:18-alpine3.16

variables:
  TEXTBOOK_FILE_NAME: textbook_v0_${CI_PIPELINE_IID}.docx
  OUTPUT_DIRECTORY: "./output"
  DIRECTORY_WITH_MARKDOWN_FILES: "./website/docs"

stages:
  - test
  - build
  - deploy
  - prepare-docx
  - release

test:
  tags:
    - iu5edu
  stage: test
  rules:
    - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH
  script:
    - cd website
    - yarn install
    - yarn build

site:
  tags:
    - iu5edu
  stage: build
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      changes:
        - website/**/*
  script:
    - cd website
    - yarn install
    - yarn build
    - mv ./build ../pages
  artifacts:
    paths:
      - pages

deploy:
  tags:
    - iu5edu-prod
  stage: deploy
  needs:
    - job: site
      artifacts: true
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      changes:
        - website/**/*
  variables:
    DEPLOY_DIR: "/home/gitlab-runner/www/devops_wiki"
  environment:
    name: production
  script:
    - mv ./pages ./public
    - rm -rf ${DEPLOY_DIR}/*
    - cp -r ./public/. ${DEPLOY_DIR}/.
  after_script:
    - rm -r ${CI_PROJECT_DIR}

build_docx:
  image: registry.gitlab.com/iu5edu/docker/docusaurus-docx-builder
  tags:
    - iu5edu
  stage: prepare-docx # This stage must run before the release stage
  rules:
    - if: $CI_COMMIT_TAG
      when: never # Do not run this job when a tag is created manually
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      changes:
        - website/blog/**/*
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH # Run this job when commits are pushed or merged to the default branch
      changes:
        - website/**/*
  script:
    - echo GE_JOB_ID=$CI_JOB_ID >> build_docx.env
    - export COMBINED_DOCX_NAME=$TEXTBOOK_FILE_NAME
    - export OUTPUT_DIRECTORY=$OUTPUT_DIRECTORY
    - export DIRECTORY_WITH_MARKDOWN_FILES=$DIRECTORY_WITH_MARKDOWN_FILES
    - python3 ./pandoc-builder.py

  artifacts:
    paths:
      - ${OUTPUT_DIRECTORY}/${TEXTBOOK_FILE_NAME}
    reports:
      dotenv: build_docx.env # Use artifacts:reports:dotenv to expose the variables to other jobs

release:
  tags:
    - iu5edu
  stage: release
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  needs:
    - job: build_docx
      artifacts: true
  rules:
    - if: $CI_COMMIT_TAG
      when: never # Do not run this job when a tag is created manually
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
      changes:
        - website/blog/**/*
      when: never
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH # Run this job when commits are pushed or merged to the default branch
      changes:
        - website/**/*
  script:
    - echo "running release_job for $TAG"
    - echo 'Previous Job ID is printed below'
    - echo $GE_JOB_ID
  release: # See https://docs.gitlab.com/ee/ci/yaml/#release for available properties
    tag_name: "v0.$CI_PIPELINE_IID" # The version is incremented per pipeline.
    description: "v0.$CI_PIPELINE_IID"
    ref: "$CI_COMMIT_SHA" # The tag is created from the pipeline SHA.
    assets:
      links:
        - name: "DOCX textbook"
          url: "${CI_PROJECT_URL}/-/jobs/${GE_JOB_ID}/artifacts/file/${OUTPUT_DIRECTORY}/${TEXTBOOK_FILE_NAME}"
